<?php
    class checking_code_paiement extends FrontEnd_Controller {

        function checking_code_paiement() {
            parent::FrontEnd_Controller();
            $this->load->helper('cookie');
            $this->load->model('Accueil_m');
            $this->load->model('Actualite_m');
            $this->load->model('Admin_m');
            $this->load->helper('date');
            $this->load->library('form_validation');
            $this->_data['error'] ='';
            $this->load->library('email');
        }
        function index() {

        }
        function check($pass){
            if($pass=="ijKlmfSpquyV"){
                $etat=$this->input->post('etat');
                $divers=$this->input->post('divers');
                $paid_amount = $this->input->post('montant');
                $array=explode('|',$divers);
                $site=$array[0];
                if($site=='zoo'){
                    $id_commande=$array[1];
                    $vip=$array[2];
                    $data = array('status'=>$etat);
                    if($etat=="ok"){
                        $query=$this->Accueil_m->get_detail_commande($id_commande);
                        foreach ($query as $aRow):
                            $quantity=$this->db->get_where('zoo_produit',array('id'=>$aRow['idproduit']))->row()->quantite;
                            $quantity_left=$quantity-$aRow['quantite'];
                            $data_produit=array('quantite'=>$quantity_left);
                            $query_produit= $this->Accueil_m->update_product($data_produit,$aRow['idproduit']);
                            endforeach;

                        if($vip==1){
                            $client=$this->db->get_where('zoo_commande',array('id'=>$id_commande))->row()->idclient;
                            $query=$this->Accueil_m->update_user(array('vip'=>0),$client);
                        }

                    }
                    $query=$this->Accueil_m->update_commande($data,$id_commande);

                }
                if($site=='animus'){
                    /*Do something*/
                    $announce_id = $array[1];
                    $announce_level = $array[2];
                    if($etat=="ok"){
                        /*Get days by level and update animus_annonce expire date*/
                        $query = $this->Accueil_m->get_day_allow_by_level($announce_level);
                        if ($query->num_rows() > 0) {
                            $d = $query->row();
                        }
                        $dc = new DateClass();
                        $dc->Add("days", (int) $d->day_allow);
                        $string_date = $dc->ToString();
                        $expried_date = human_to_unix($string_date);
                        /*Viet ham tren model*/
                        $this->db->update('animus_annonce',array('level'=>$announce_level,'date_expried'=>$expried_date,'active'=>1),array('id'=>$announce_id));    
                        /*VIP point calculation*/
                        $user_id = $this->db->get_where('animus_annonce',array('id'=>$announce_id))->row()->posted_by;
                        $vip_point_gain = $paid_amount>=10?($paid_amount/10):0;
                        $current_point = $this->db->get_where('lehu_users',array('id'=>$user_id))->row()->vip;
                        $new_point = $current_point+$vip_point_gain;
                        $this->db->update('lehu_users',array('vip'=>$new_point),array('id'=>$user_id));
                    }
                }
                if($site=='seatting'){
                    /*Do something*/
                    $seatting_id = $array[1];
                    if($etat=="ok"){
                        $this->db->update('seating_booking',array('status'=>1),array('id'=>$seatting_id));

                        $user_data = $this->Accueil_m->get_user_by_id($this->session->userdata('user_id'));
                        $booking= $this->Accueil_m->get_booking_by_id($seatting_id);
                        if ($user_data->num_rows() > 0) {
                            $user = $user_data->row();
                            $From = $user->email;
                            $to = 'administrator@lehu.com';
                            $this->email->from($From, 'Seatting-falin');
                            $this->email->to($to);
                            $this->email->subject('Booking gardien');
                            $add_fi = $booking['add_fidelity']==1?'Qui':'Non';
                            $medi = $booking['medicaments']==1?'Qui':'Non' ;

                            $message = 'Hello ' . $user->username . ".<p> Il s'agit d'une des nouvelles commandes gardien de Lehu</p>";
                            $message .= '<p style="font-weight:bold;">' . $booking['title'] . '</p>';
                            $message .= '<p>    - In city: ' . $city . '</p>';
                            $message .= '<p>    - A partir de quelle date :' . $booking['date_start'] . '</p>';
                            $message .= '<p>    - Combien de jours : ' . $booking['total_date'] . ' jours</p>';
                            $message .= '<p>    - A partir de quelle hour: ' . $booking['hour_start'] . 'h</p>';
                            $message .= '<p>    - Combien de hours : ' . $booking['total_hour'] . 'h</p>';
                            $message .= '<p>    - Animal kind: ' . $booking['animal'] . '</p>';
                            $message .= '<p>    - Total animal: ' . $booking['total_animal'] . '</p>';
                            //$message .= '<p>    - Faudra t-il que le gardien lui administre des médicaments durant la mission: ' . $medi. '</p>';
                            $message .= '<p>    - Comment avez-vous connu Seatting-falin: ' . $booking['known'] . '</p>';
                            $message .= '<p>    - Votre/vos animaux présentent t-ils des caractéristiques particulières au niveau de son/leurs comportements: ' . $booking['content'] . '</p>';
                            $this->email->message($message);
                            //echo $message;die;
                            $this->email->send();
                            //end send
                        }


                    }
                }
            }
        }
} ?>
